﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;


namespace BasicDataManagement
{
    /// <summary>
    /// AddOptionItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddOptionItemWindow : Window
    {
        public ObservableCollection<string> Cats { get; set; }
        public string DefaultCat { get; set; }

        public ObservableCollection<Ingredient> MaterialList { get; set; }

        private IOpertation op = OperationFactory.Create();

        public OptionObject optionObject { get; set; }

        public AddOptionItemWindow()
        {
            InitializeComponent();

            MaterialList = new ObservableCollection<Ingredient>();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (optionObject == null)
            {
                optionObject = new OptionObject();
            }

            catListCombo.ItemsSource = Cats;
            catListCombo.SelectedIndex = Cats.IndexOf(optionObject.BelongedCategoryName);

            nameTB.Text = optionObject.ObjectName;
            unitTB.Text = optionObject.Unit;

            MaterialList = new ObservableCollection<Ingredient>(optionObject.IngredientList);
            listViewMaterial.ItemsSource = MaterialList;
        }

        private void btnAddMaterial_Click(object sender, RoutedEventArgs e)
        {
            //AddIngredientWindow win = new AddIngredientWindow();
            //win.Cats = new ObservableCollection<string>(op.LoadMateriaclItemName());
            //win.Owner = this;
            //win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //if (win.ShowDialog() == true)
            //{
            //    MaterialList.Add(win.IngredientResult);
            //}
            AddSubItemWindow win = new AddSubItemWindow();
            win.ingredientList = op.LoadMaterialSimpleObjects();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (win.ShowDialog() == true)
            {
                MaterialList.Add(win.IngredientResult);
            }
        }

        private void btnDelMaterial_Click(object sender, RoutedEventArgs e)
        {
            if (listViewMaterial.SelectedItem != null)
            {
                MaterialList.Remove(listViewMaterial.SelectedItem as Ingredient);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            optionObject.ObjectName = nameTB.Text;
            optionObject.Unit = unitTB.Text;
            optionObject.BelongedCategoryName = catListCombo.Text;
            optionObject.IngredientList = MaterialList.ToList();

            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

    }
}
